package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.domain.JingfeiPici;
import com.ruoyi.system.mapper.DeptMapper;
import com.ruoyi.system.mapper.JingfeiDeptMapper;
import com.ruoyi.system.mapper.JingfeiDetailMapper;
import com.ruoyi.system.mapper.JingfeiPiciMapper;
import com.ruoyi.system.service.IJingfeiDeptService;
import com.ruoyi.system.service.IJingfeiPiciService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 经费管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-10
 */
@Service
public class JingfeiPiciServiceImpl implements IJingfeiPiciService 
{
    @Autowired
    private JingfeiPiciMapper jingfeiPiciMapper;

    @Autowired
    private JingfeiDeptMapper jingfeiDeptMapper;

    @Autowired
    private JingfeiDetailMapper jingfeiDetailMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private IJingfeiDeptService jingfeiDeptService;

    /**
     * 查询经费管理
     * 
     * @param id 经费管理主键
     * @return 经费管理
     */
    @Override
    public JingfeiPici selectJingfeiPiciById(String id)
    {
        return jingfeiPiciMapper.selectJingfeiPiciById(id);
    }

    /**
     * 查询经费管理列表
     * 
     * @param jingfeiPici 经费管理
     * @return 经费管理
     */
    @Override
    public List<JingfeiPici> selectJingfeiPiciList(JingfeiPici jingfeiPici)
    {
        return jingfeiPiciMapper.selectJingfeiPiciList(jingfeiPici);
    }

    /**
     * 新增经费管理
     * 
     * @param jingfeiPici 经费管理
     * @return 结果
     */
    @Override
    public int insertJingfeiPici(JingfeiPici jingfeiPici)
    {
        return jingfeiPiciMapper.insertJingfeiPici(jingfeiPici);
    }

    /**
     * 修改经费管理
     * 
     * @param jingfeiPici 经费管理
     * @return 结果
     */
    @Override
    public int updateJingfeiPici(JingfeiPici jingfeiPici)
    {
        return jingfeiPiciMapper.updateJingfeiPici(jingfeiPici);
    }

    /**
     * 批量删除经费管理
     * 
     * @param ids 需要删除的经费管理主键
     * @return 结果
     */
    @Override
    public int deleteJingfeiPiciByIds(String ids)
    {
        return jingfeiPiciMapper.deleteJingfeiPiciByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除经费管理信息
     * 
     * @param id 经费管理主键
     * @return 结果
     */
    @Override
    public int deleteJingfeiPiciById(String id)
    {
        return jingfeiPiciMapper.deleteJingfeiPiciById(id);
    }

    /**
     * 计算批次经费
     *
     * @param jingfeiPici 当前经费批次
     * @return
     */
    @Override
    public int jisuanPiciJingfei(JingfeiPici jingfeiPici) {

        return jingfeiDeptService.jisuandanweijingfei(jingfeiPici);

    }

    @Override
    public List<Map<String, Object>> findCustomList(String ppDeptId, String ppPicimingcheng) {
        ppPicimingcheng = "%" + ppPicimingcheng + "%";
        return jingfeiPiciMapper.findCustomList(ppDeptId, ppPicimingcheng);
    }

    @Override
    public List<Map<String, Object>> findCustomListHistory(String ppDeptId, String ppPicimingcheng) {
        ppPicimingcheng = "%" + ppPicimingcheng + "%";
        return jingfeiPiciMapper.findCustomListHistory(ppDeptId, ppPicimingcheng);
    }

    @Override
    public List<Map<String, Object>> findCustomListNow(String ppDeptId, String ppPicimingcheng) {
        ppPicimingcheng = "%" + ppPicimingcheng + "%";
        return jingfeiPiciMapper.findCustomListNow(ppDeptId, ppPicimingcheng);
    }

    @Transactional
    @Override
    public int addCustom(JingfeiPici jingfeiPici) {
        int ok = 1;

        if (insertJingfeiPici(jingfeiPici)!=1){
            ok = 0;
            throw new RuntimeException("添加经费批次失败");
        }

        return ok;
    }

    @Transactional
    @Override
    public int modifyCustom(JingfeiPici jingfeiPici) {
        int ok = 1;

        if (updateJingfeiPici(jingfeiPici)!=1){
            ok = 0;
            throw new RuntimeException("添加经费批次失败");
        }

        return ok;
    }

}
